package licode.unisop.oidc.authsa.conf;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaStrategy;
import licode.unisop.oidc.auth.gateway.AuthorCache;
import licode.unisop.oidc.auth.stu.AuthorUtil;
import licode.unisop.oidc.authsa.service.AuthorCacheDefault;
import licode.unisop.oidc.authsa.service.AuthorDelegateImpl;
import licode.unisop.oidc.authsa.service.SaSessionProxy;
import licode.unisop.oidc.authsa.service.SaTokenDaoProxy;
import licode.unisop.oidc.authsa.stp.SaStpLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author licode
 */
@Configuration
public class AuthAutoConfig {
    public AuthAutoConfig(@Autowired(required = false) AuthorCache authorCache) {
        if (null != authorCache) {
            AuthorUtil.setCache(authorCache);
        } else {
            AuthorUtil.setCache(new AuthorCacheDefault());
        }

        AuthorUtil.setLoginDelegate(new AuthorDelegateImpl());
        SaStrategy.instance.setCreateSession(SaSessionProxy::new);
        StpUtil.setStpLogic(new SaStpLogic(StpUtil.TYPE));
    }

    @Bean
    public SaTokenDao getSaTokenDao() {
        return new SaTokenDaoProxy(AuthorUtil.getCache());
    }
}
